import {getRequest} from "@/utils/api";

/**
 * 菜单初始化  如果有值直接返回   否则判断值给到Vuex
 * @param router
 * @param store
 */
export const initMenu=(router,store)=>{
    if(store.state.routes.length > 0){
        return;
    }
    getRequest('/system/config/menu').then(data=>{
        if(data){
            let fmtRoutes = formatRoutes(data);
            router.addRoutes(fmtRoutes);
            store.commit('initRoutes',fmtRoutes);
        }
    })
}

export const formatRoutes = (routes)=>{
    let fmRoutes = [];
    routes.forEach(router=>{
        let{
            path,component,name,meta,iconCls,children
        } = router;
        if(children && children instanceof Array){
            children = formatRoutes(children);
        }
        let fmRouter = {
            path:path,
            name:name,
            meta:meta,
            iconCls:iconCls,
            children:children,
            component(reslove){
                if(component.startsWith("Home")){
                    require(['../views/'+component+'.vue'],reslove);
                }else if(component.startsWith("Emp")){
                    require(['../views/emp/'+component+'.vue'],reslove);
                }else if(component.startsWith("Per")){
                    require(['../views/per/'+component+'.vue'],reslove);
                }else if(component.startsWith("Sal")){
                    require(['../views/sal/'+component+'.vue'],reslove);
                }else if(component.startsWith("Sta")){
                    require(['../views/sta/'+component+'.vue'],reslove);
                }else if(component.startsWith("Sys")){
                    require(['../views/sys/'+component+'.vue'],reslove);
                }

            }
        }
        fmRoutes.push(fmRouter);
    })
    return fmRoutes;
}